// pages/zpsc/worksList/worksList.js
var app = getApp();
const util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winH: null,
    value: '',
    isOver: false,
    leftList: [],
    rightList: [],
    leftHight: 0,
    rightHight: 0,
    pageNum: 1,
    pageSize: 6,
    sw: "",
    activityId: null,
    currentTab:0
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function(options) {
    console.log(options);
    this.setData({
      winH: wx.getSystemInfoSync().windowHeight - 60,
      activityId: options.activityId
    })
    console.log(this.data.noramalData)
    this.getProductList()
  },
  getProductList: function() {
    wx.showLoading({
      title: '加载中...',
    })
    var that = this;
    wx.request({
      url:  app.globalData.activityHost + '/services/activityaction/listActivityProductionWx',
      data: {
        activityId: that.data.activityId,
        currentPage: that.data.pageNum,
        pageSize: that.data.pageSize,
        sw: that.data.sw
      },
      success: function (res) {
        console.log(res);
        if (res.data.result == "0") {
          var allData = res.data.obj.data;
          //定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
          var leftH = that.data.leftHight;
          var rightH = that.data.rightHight;
          var leftData = [];
          var rightData = [];
          for (let i = 0; i < allData.length; i++) {
            var currentItemHeight = parseInt(Math.round(allData[i].coverHeight * 340 / allData[i].coverWidth));
            allData[i].coverHeight = currentItemHeight + "rpx";
            if (leftH == rightH || leftH < rightH) {
              leftData.push(allData[i]);
              leftH += currentItemHeight;
            } else {
              rightData.push(allData[i]);
              rightH += currentItemHeight;
            }
          }
          wx.hideLoading()

          //更新左右两栏的数据以及累计高度
          that.setData({
            leftHight: leftH,
            rightHight: rightH,
            leftList: [...that.data.leftList, ...leftData],
            rightList: [...that.data.rightList, ...rightData],
            pageNum: that.data.pageNum + 1
          })

          // that.setData({
          //   list: that.data.concat(res.data.obj),
          //   currentPage: that.data.currentPage + 1
          // })

        } else {
          console.log("请求活动详情接口异常");
          wx.hideLoading()
        }
      },
      fail: function () {
        console.log("获取活动详情出错！")
        wx.hideLoading()
      }
    })

    // wx.request({
    //   url: 'https://bookshequ3.chaoxing.com/classify.html',
    //   data: {
    //     callback: 'json',
    //     cid: 1529,
    //     pageNum: that.data.pageNum,
    //     pageSize: that.data.pageSize
    //   },
    //   success: (res) => {
    //     var that = this;
    //     var allData = data;
    //     //定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
    //     var leftH = that.data.leftHight;
    //     var rightH = that.data.rightHight;
    //     var leftData = [];
    //     var rightData = [];
    //     for (let i = 0; i < allData.length; i++) {
    //       var currentItemHeight = parseInt(Math.round(allData[i].CoverHeight * 340 / allData[i].CoverWidth));
    //       allData[i].CoverHeight = currentItemHeight + "rpx";
    //       if (leftH == rightH || leftH < rightH) {
    //         leftData.push(allData[i]);
    //         leftH += currentItemHeight;
    //       } else {
    //         rightData.push(allData[i]);
    //         rightH += currentItemHeight;
    //       }
    //     }
    //     wx.hideLoading()

    //     //更新左右两栏的数据以及累计高度
    //     that.setData({
    //       leftHight: leftH,
    //       rightHight: rightH,
    //       leftList: [...that.data.leftList, ...leftData],
    //       rightList: [...that.data.rightList, ...rightData],
    //       pageNum: that.data.pageNum + 1
    //     })
    //   }
    // })
  },
  onChange: function(e) {
    this.setData({
      sw: e.detail
    })
  },
  onSearch: function() {
    console.log(1)
    this.setData({
      pageNum: 1,
      leftList: [],
      rightList: [],
      leftHight: 0,
      rightHight: 0,
      isOver: false
    })
    this.getProductList();
  },
  onClick: function() {
    this.onSearch()
  },
  scrolltolower: function() {
    this.getProductList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let url = util.getCurrentPageUrlWithArgs();
    if (url.indexOf('?') > -1) {
      url += '&fid=' + app.globalData.fid + '&fidEnc=' + app.globalData.fidEnc;
    } else {
      url += '?fid=' + app.globalData.fid + '&fidEnc=' + app.globalData.fidEnc;
    }
    if (options.from == "menu") {
      return {
        title: app.globalData.site.name,
        path: url
      }
    }
  }
})